﻿Imports AForge.Imaging.Filters
Imports AForge
Imports AForge.Imaging
Imports UCBase

Public Class UniversalColorFilter
    Implements I_ImageExchange
    Private WithEvents Inp As I_ImageExchange

    Private Backpic As New BackgroundCreator
    Private Mov As New Mover

    Private SourceBitmap As System.Drawing.Bitmap
    Private DestBitmap As System.Drawing.Bitmap

    Private Stat As ImageStatistics
    Private histo As AForge.Math.Histogram = Nothing


    Private Sub ColorFilter()
        Dim filter As LevelsLinear = New LevelsLinear()
        filter.OutRed = New IntRange(TrackBar6.Value, TrackBar1.Value)
        filter.OutGreen = New IntRange(TrackBar5.Value, TrackBar2.Value)
        filter.OutBlue = New IntRange(TrackBar4.Value, TrackBar3.Value)
        'filter.InRed = New IntRange(0, TrackBar6.Value)
        'filter.InGreen = New IntRange(0, TrackBar5.Value)
        'filter.InBlue = New IntRange(0, TrackBar4.Value)

        DestBitmap = filter.Apply(SourceBitmap)
        RaiseEvent ImageOut(DestBitmap)
    End Sub

    Private Sub TrackBar6_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar6.Scroll
        ColorFilter()
    End Sub

    Private Sub TrackBar5_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar5.Scroll
        ColorFilter()
    End Sub

    Private Sub TrackBar4_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar4.Scroll
        ColorFilter()
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        ColorFilter()
    End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        ColorFilter()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(Me.Width, Me.Height)
        Mov.Connect(Me)

    End Sub


    Public Event ImageOut(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1

    Private Sub Socket1_Connect() Handles Socket1.Connect
        Dim S As Socket = Socket1.Remote
        Inp = S.Parent
    End Sub

    Private Sub UniversalColorFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '518, 469
        Me.Width = 520
        Me.Height = 470
    End Sub

    Private Sub Inp_ImageOut(ByVal Image As System.Drawing.Bitmap) Handles Inp.ImageOut1
        SourceBitmap = Image

        Stat = New AForge.Imaging.ImageStatistics(SourceBitmap)
        histo = Stat.Red
        Label2.Text = histo.Values.Count
        Histogram1.Values = histo.Values

        ColorFilter()
    End Sub

    Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar3.Scroll
        ColorFilter()
    End Sub
End Class
